package com.chinasofti.info_manager.service;

import com.chinasofti.info_manager.dao.BaseStudentDao;
import com.chinasofti.info_manager.dao.OtherStudentDao;
import com.chinasofti.info_manager.domain.Student;
import com.chinasofti.info_manager.facotry.StudentDaoFactory;

public class StudentService {

//    OtherStudentDao Dao = new OtherStudentDao();
    private BaseStudentDao Dao = StudentDaoFactory.getStudentDao();


    public void deleteStudentById(String id){
        Dao.deleteStudentById(id);
    }

    //判断学号是否存在
    public boolean isExists(String id){ //返回值weitrue代表 id不存在
        boolean exists = false;
        //判断数组中是否有这个id
        int index = Dao.getIndex(id);
        if(index == -1){
            exists = true; //不存在
        }
        return exists;
    }

    public boolean addStudent(Student stu) {
        return Dao.addStudent(stu);
    }

    public Student[] findAllStudent() {
        Student[] allStudents = Dao.findAllStudent();
        boolean flag = false;//做一个开关；allStudents有值怎返回；allStudents没有值返回null
        for (int i=0;i<allStudents.length;i++){
            Student stu = allStudents[i];
            if(stu != null){
                flag = true;
                break;
            }
        }
        if(flag){
            return allStudents;
        }else {
            return null;
        }

    }

    /**
     *
     * @param updateId
     * @param newStu 更新对象
     * @return
     */
    public boolean updateStudent(String updateId, Student newStu) {
        Dao.updateStudent(updateId,newStu );
        return true;
    }
}